<?php
/**
 * Модель article
 * 
 * Класс модели
 * @author Anton Zavidov
 * @package myLiveShop
 */

/**
 * Модель article
 *
 */
class ModelArticle extends Model {
	
	const TABLE				= 'article';
	const F_ARTICLE_ID  	= 'article_id';
	const F_SHOP_ID  		= 'shop_id';
	const F_USER_ID  		= 'user_id';
	const F_LANG_ID  		= 'lang_id';
	const F_CATEGORY_ID		= 'category_id';
	const F_IMAGE_ID  		= 'image_id';
	const F_MODEL			= 'model';
	const F_DELETED			= 'deleted';
	const F_CREATED			= 'created';
	const F_PUBLISH			= 'publish';
	const F_SOURCE			= 'source';
	const F_DESCRIPTION		= 'description';
	const F_TITLE			= 'title';
	
	public function __construct($className=null){
		parent::__construct(__CLASS__);
		$this->name='article';
		$this->id_field=array(self::F_ARTICLE_ID);
		$this->fields=array(
			self::TABLE.DOT.self::F_ARTICLE_ID 		=> new Field($this,self::F_ARTICLE_ID,self::TABLE,array(
										'lenght'=>11,
										'auto'=>true)),
			self::TABLE.DOT.self::F_SHOP_ID  		=> new Field($this,self::F_SHOP_ID,self::TABLE,array(
										'lenght'=>9,
										'auto'=>true,
										'join_table'=>'shop')),
			self::TABLE.DOT.self::F_USER_ID 		=> new Field($this,self::F_USER_ID,self::TABLE,array(
										'lenght'=>11,
										'important'=>true)),
			self::TABLE.DOT.self::F_CATEGORY_ID 		=> new Field($this,self::F_CATEGORY_ID,self::TABLE,array(
										'lenght'=>11,
										'auto'=>true,
										'join_table'=>'category')),
			self::TABLE.DOT.self::F_LANG_ID 		=> new Field($this,self::F_LANG_ID,self::TABLE,array(
										'lenght'=>2,
										'auto'=>true,
										'important'=>true,
										'join_table'=>'lang')),
			self::TABLE.DOT.self::F_MODEL			=> new Field($this,self::F_MODEL,self::TABLE,array(
										'type'=>Field::TYPE_STRING,
										'important'=>true,
										'lenght'=>20)),
			self::TABLE.DOT.self::F_IMAGE_ID 		=> new Field($this,self::F_IMAGE_ID,self::TABLE,array(
										'lenght'=>5,
										'auto'=>true,
										'join_table'=>'image')),
			self::TABLE.DOT.self::F_DELETED 		=> new Field($this,self::F_DELETED,self::TABLE,array(
										'validate_type'=>Validator::VALIDATE_BOOL)),
			self::TABLE.DOT.self::F_CREATED 		 =>new Field($this,self::F_CREATED,self::TABLE,array(
										'type'=>Field::TYPE_DATE)),
			self::TABLE.DOT.self::F_PUBLISH 	 	=> new Field($this,self::F_PUBLISH,self::TABLE,array(
										'type'=>Field::TYPE_STRING,
										'validate_type'=>Validator::VALIDATE_HASH,
										'lenght'=>1)),
			self::TABLE.DOT.self::F_SOURCE=> new Field($this,self::F_SOURCE,self::TABLE,array(
										'type'=>Field::TYPE_TEXT)),
			self::TABLE.DOT.self::F_DESCRIPTION 	=> new Field($this,self::F_DESCRIPTION,self::TABLE,array(
										'type'=>Field::TYPE_TEXT)),
			self::TABLE.DOT.self::F_TITLE 			=> new Field($this,self::F_TITLE,self::TABLE,array(
										'type'=>Field::TYPE_STRING,
										'important'=>true,
										'lenght'=>255))
		);
	}
	public function getAllTags($article_id){
		import("model.tag");
		$tag_to_article = import("model.tag_to_article",true);
		return $tag_to_article->load(null,array(ModelTag::F_TAG_ID, ModelTag::TABLE.DOT.ModelTag::F_TITLE),array(
			'where'=>ModelTag_to_article::F_ARTICLE_ID .'='.$article_id,
			'join'=>array(
					array('table'=>ModelTag::TABLE, 'key'=>ModelTag::F_TAG_ID, 'join_table'=>ModelTag_to_article::TABLE, 'join_field'=>ModelTag_to_article::F_TAG_ID)
			)
		));
	}
}